﻿var lsInfo = {
  current: 1,
  pageSize: allPageSize,
  data: [],
  totalRow: 0,
  totalPage: 0,
  oneDate: {},
  editState: false
};
var projectId = getUrlId();
// document.getElementById('nav').innerHTML = template('navs', navinfo1);

$('#nav li').eq(2).addClass('active');

allCost();
function allCost() {
  $.ajax({
    cache: false,
    async: false,
    url: '/api/CommonApi?API=GetProjectCountMoney',
    type: 'get',
    data: {
      ProjectID: projectId
    },
    dataType: 'json',
    success: function (data) {
      // console.log(data)
      if (data.Success) {
        console.log(data);
        var cost = data.ApiParamObj[0].CountMoney;
        $('#allCost').html(cost);
      }
    }
  });
}

var getList = function (obj) {
  $.ajax({
    cache: false,
    async: false,
    url: '/api/CommonApi?API=PDGetProjectsByCostDetail',
    type: 'get',
    data: {
      ProjectID: projectId,
      sorts: ' CreateTime DESC',
      pagesize: obj.size,
      page: obj.index
    },
    dataType: 'json',
    success: function (data) {
      // console.log(data)
      if (data.Success) {
        obj.callback(data)
      }
    }
  });
};

getList({
  size: lsInfo.pageSize,
  index: lsInfo.current,
  callback: function (data) {
    console.log(data)
    // console.log(JSON.stringify(data));
    var newData = data.ApiParamObj[0];
    for (var key in newData) {
      newData[key].CostDate = new Date(newData[key].CostDate).pattern('yyyy/MM/dd');
      if (newData[key]['State'] === '????') {
        newData[key]['State'] = '';
      }
    }
   /* var i = 0;
    var l = newData.length;
    var allCost = 0;
    for (;i < l;i++) {
      allCost += parseInt(newData[i].Money, 10);
    }*/
    // document.getElementById('allCost').innerHTML = '项目成本合计：' + allCost + '元';
    // 列表数据
    document.getElementById('listTable').innerHTML = template('l43List', newData);
    // 分页
    // var pagesInfo = data.ApiParamObj[1];
    // lsInfo.data = newData;
    // lsInfo.totalRow = pagesInfo[0].totalRow;
    // lsInfo.totalPage = pagesInfo[0].totalPage;

    // $('#totalRow').html(lsInfo.totalRow);
    // pagesSet(lsInfo);
    // setPageSize(lsInfo);
    loading.hide();
  }
});

// 点击分页请求事件
/*var showPage = function (o) {
  // console.log(o)
  getList({
    size: o.pageSize,
    index: o.current,
    callback: function (data) {
      // console.log(data)
      // console.log(JSON.stringify(data));
      var newData = data.ApiParamObj[0];
      for (var key in newData) {
        if (newData[key]['State'] === '????') {
          newData[key]['State'] = '';
        }
      }
      // 列表数据
      document.getElementById('listTable').innerHTML = template('l43List', newData);
      // 分页
      var pagesInfo = data.ApiParamObj[1];
      o.data = newData;
      o.totalRow = pagesInfo[0].totalRow;
      o.totalPage = pagesInfo[0].totalPage;

      $('#totalRow').html(o.totalRow);
      pagesSet(o);
      loading.hide();
    }
  });
};*/

$('#table').on('click', '.view', function () {
  var index = $(this).parent().parent().index();
  var v = lsInfo.data[index];
  window.open('/PD/Project/indexl13/' + v.ProjectID, '_blank');
  console.log(v);
  return false;
});